﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class ProductSearchGetHander : IRequestHandler<ProductSearchGetCommand, ResultPage<Product>>
    {

        private readonly IBaseRepositorys<Product> productRepository;

        public ProductSearchGetHander(IBaseRepositorys<Product> productRepository)
        {
            this.productRepository = productRepository;
        }

        /// <summary>
        /// Get方法查询产品信息
        /// </summary>
        /// <param name="request">查询产品信息请求对象</param>
        /// <param name="cancellationToken">允许异步</param>
        /// <returns>查询产品信息响应对象</returns>
        public Task<ResultPage<Product>> Handle(ProductSearchGetCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultPage<Product>();
                var list = productRepository.GetAll().Where(c => !c.DeleteDate);
                result.totalCount = list.Count();
                result.totalPage = (int)Math.Ceiling((double)result.totalCount / request.PageSize);
                result.Data = list.OrderBy(c => c.ProductId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                return Task.FromResult(result);


            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
